<?php
/*
 * STOK.KODE.HP.JMl.PIN
 * STOK.KODE.HP1.JML.HP2.JML.HP3.JML.PIN
 *
 */
echo 'Transaksi Stok';

$interval = 2;
$set_auto = "";
$v_sisa = 1;
$v_jmlarr = 5;

$jmlarr = count($prm);
$sisa = $jmlarr % 2;

$v_pin = (1) ? $delimiter.$prm[$jmlarr-1] : "";
$v_min = (1) ? 2 : 1;

$v_opr = ($row->xopr) ? $row->xopr : "otomatic";

/*
 *
 */
if (($jmlarr > $v_jmlarr)) {
	if ($sisa == $v_sisa) {
		//trx multi dipecah menjadi trx tunggal.
		for ($i=2;$i<$jmlarr-$v_min;$i=$i+2) {
			qinsert("inbox","in_hpnumber, in_message","'$hp',
            '".$prm[0].$delimiter.$prm[1].$delimiter.$prm[$i].$delimiter.$prm[$i+1].$v_pin."'",1,1);
			echo $prm[0].$delimiter.$prm[1].$delimiter.$prm[$i].$delimiter.$prm[$i+1].$v_pin."<br>";
		}
	} else {
		//salah format
		send_sms($agenid, "Format untuk sms $message salah", 0, $_BPerintahSalah, 1);
	}
} else {
	$c_hp = $prm[1];
	$hpx = ltrim($c_hp);
	$cek0 = substr($hpx,0,1);
	$stdownline = cek_downline($c_hp, $agenid);
	$pinx = rtrim(ltrim($prm[count($prm)-1]));


	$c_nom = $prm[$i];
	$c_jml = (int) $prm[($i+1)];
	$vtype = $prm[$i];

	echo "c_nom: $c_nom, c_jml $c_jml, vtype: $vtype <br>";

	$ketx = (is_numeric($k_ulang) && ($k_ulang > 1)) ? "reorder $k_ulang." : "ke 1.";

	$b=0;
	$sql_vtype = "select * from voucher where vtype = '$vtype'";
	$rhrg = mysql_fetch_object(mysql_query($sql_vtype));

	$hrg = $rhrg->harga * $c_jml;

	$ada = $rhrg->status;
	$opr = $rhrg->opr;
	$setsukses = $rhrg->sukses;

	if($setsukses) {
		$v_s = (is_numeric($k_ulang) && ($k_ulang > 1)) ? $k_ulang : 1;
		$ketx="sukses depan ke ".$v_s.".";
	}

	$ket_voc = (is_numeric(substr($c_nom,0,1))) ? $rhrg->ket : $prm[1];
	$ket_voc .= " ($c_jml) ";
	$b = ($kelompok == "khusus" || $kelompok == "server") ? 0 : $b;

	$sisaLimit = ceksisa($agenid, $vtype);

	if(empty($pinx)) {
		$sms = "Maaf Pin Kosong ";
		send_sms($agenid, $sms, 1,0,1,2);
	}elseif($cek0 != '0'){
		$sms = "Format HP salah, koreksi ulang.";
		send_sms($agenid, $sms, 1,0,1,2);
	}elseif ($c_jml <= 0) {
		$sms = "Maaf jumlah order ($c_jml) tidak diizinkan ";
		send_sms($agenid,$sms, 0, $_bPerintahSalah, 1);
	}/* elseif ($sisaLimit < $c_jml) {
	$limit = limitAgen($agenid, $vtype);
	$sms = isi_sms("[nama],[limit],[antrian],[sisalimit],[voc],[jml],[harga]",
	"$agenid,$limit,$antrian,$sisaLimit,$c_nom,$c_jml,$hrg",$_GPesan['StokTidakCukup']);
	send_sms($agenid,$sms, 0, 0, 1,2);
	}*/
	elseif ($ada) {
		$sms = isi_sms("[voc],[hp]","$ket_voc,$hpx",$_GPesan['Kosong']);
		send_sms($agenid,$sms, 1, 0, 1,2);
	} elseif (!$stdownline) {
		$sms = "Maaf nomor $hpx tidak menjadi downline anda ";
		send_sms($agenid,$sms, 1, 0, 1,2);
	}/* elseif ($gangguan) {
	$sms = isi_sms("[voc],[hp]","$ket_voc,$hpx",$_GPesan['Gangguan']);
	send_sms($agenid,$sms , 1, 0, 1,2);
	} */
	else {
		$intorder = ($_GIntervalOrder) ? " (DATE_ADD(tanggal, INTERVAL $_GIntervalOrder HOUR) > NOW())" : " (DAYOFYEAR(now())=DAYOFYEAR(tanggal))";

		$rstmp = s_query("tanggal, tujuan, status, jml, vsn","purchase","(tujuan = ".$hpx.") and (vtype = '$vtype') and (agenid = '$agenid')
					and (jml = $c_jml) and (status <> 2) and (status <> 3) 
					and (status <> 5) and (ket='$ketx') and $intorder");

		if (mysql_num_rows($rstmp) > 0) {
			$rtmp = mysql_fetch_object($rstmp);
			$status = the_status($rtmp->status);
			$zz = ($rtmp->status == 4) ?  "SN:$rtmp->vsn" : "Mhon Tunggu";
			$sms = str_replace("[hp]",$hpx, "PENGULANGAN");
			$sms = str_replace("[voc]",$ket_voc,$sms);
			$sms = str_replace("[jml]",$c_jml,$sms);
			$sms = str_replace("[tanggal]",$rtmp->tanggal,$sms);
			$sms = str_replace("[status]",$status,$sms);
			$sms = str_replace("[ket]",$zz,$sms);

			send_sms($agenid, $sms, 0, $_BPengulangan, 1,2);
		} else {
			if(qinsert("purchase","tanggal, agenid, vtype, tujuan, jml, harga, bonus, ket, ket_voc","NOW(), '$agenid', '$vtype', '$hpx', '$c_jml', '$hrg', $b, '$ketx','$ket_voc'",1)) {
				echo "Insert New Trx ELEKTRIK $prm[1] OK...<hr noshade>";

			}

			/*$ketagen = "PRC:$vtype $ket_voc $c_jml,$hp,$vsn [sukses depan]";
			 update_salagen($agenid, $ketagen, -$hrg);
			 $sawal=$sisaLimit+$hrg;
			 $sms = isi_sms("[hp],[jml],[voc],[vsn],[harga],[awal],[akhir]","$hpx,0,$ket_voc,$xsn,$hrg,$sawal,$sakhir",$_GPesan['TrxSukses']);
			 send_sms ($agenid, $sms, 1, 0, 0,$hp);*/


		} // jika syarat2 sdh ok
	} // jika bukan multi order ...
} // if gol = voucher
$smspurchase++;
?>
